<?php
declare (strict_types = 1);

namespace app\common\controller\upload;

use app\BaseController;
use app\common\model\AttachmentModel;
use app\common\service\UploadService;

/**
 * 上传接口
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class Api extends BaseController
{

    /**
     * 上传图片接口
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function uploadImage(){
        $uploadService = new UploadService();
        $res = $uploadService->uploadImage();
        if(!$res){
            return self::createFail($uploadService->getError());
        }else{
            $data = AttachmentModel::where('aid',$res->id)->field('aid,filepath,fileurl')->find();
            return self::createSuccess('上传成功',$data);
        }
    }

    /**
     * 上传视频接口
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function uploadVideo(){
        $uploadService = new UploadService();
        $res = $uploadService->uploadVideo();
        if(!$res){
            return self::createFail($uploadService->getError());
        }else{
            $data = AttachmentModel::where('aid',$res->id)->field('aid,filepath,fileurl')->find();
            return self::createSuccess('上传成功',$data);
        }
    }



    /**
     * 上传文件接口
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function uploadFile(){
        $uploadService = new UploadService();
        $res = $uploadService->uploadFile();
        if(!$res){
            return self::createFail($uploadService->getError());
        }else{
            $data = AttachmentModel::where('aid',$res->id)->field('aid,filepath,fileurl')->find();
            return self::createSuccess('上传成功',$data);
        }
    }
    
}
